package com.zhongyi.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;



import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 自定义JDBC辅助工具类
 * <p>
 * 
 *
 * @author <a href="mailto:lysongfei@gmail.com">songfei</a>
 * @version 3.0, 2012-4-24
 */
public class DBUtil {
	
	public static Logger logger = Logger.getLogger (DBUtil.class) ; 


	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection(){
		Connection conn = null;
		try {
			ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(HttpUtil.getServletContext());
//			ApplicationContext ac = new FileSystemXmlApplicationContext("/WebContent/WEB-INF/classes/dataAccessContext-local.xml");
			BasicDataSource dataSource = (BasicDataSource)ac.getBean("dataSource");
			conn = dataSource.getConnection();
		}  catch (SQLException e) {
			logger.error("error getting the database connection ... ",e);
		}
		return conn;
	}

}
